<?php
global $AppUI, $m, $tab;
// Get login object
$uid = $AppUI->user_id;
// Load quotation object
$obj = new CQuotations();
$quote_id = intval(dPgetParam($_GET, "quote_id", 0));
$item_id = intval(dPgetParam($_GET, "item_id", 0));
// TODO: Check permission role
// Check edit
$perms =& $AppUI->acl();
$canView = $perms->checkModuleItem($m, 'view');
$canEdit = $perms->checkModuleItem($m, 'edit');
//$canView = 1;
// check permission list quotations
if(!$canView){
	$AppUI->setMsg('View Permission Deny', UI_MSG_ERROR);
	$AppUI->redirect();
}
$AppUI->savePlace();
$df = $AppUI->getPref('SHDATEFORMAT');
$pathCompanyView = './index.php?m=companies&a=view';

if (isset($_GET['tab'])) {
	$AppUI->setState('QuotationsIdxTab', $_GET['tab']);		// saves the current tab box state
}
$tab = $AppUI->getState('QuotationsIdxTab') !== NULL ? $AppUI->getState('QuotationsIdxTab') : 0;
$active = intval(!$AppUI->getState('QuotationsIdxTab'));

// setup the title block with Name, Icon and Help
$titleBlock = new CTitleBlock('Quotations', 'quotations.jpg', $m, "$m.$a");
$titleBlock->addCell();
$titleBlock->addCell(
	'<input type="submit" class="button" value="'.$AppUI->_('quotation list').'">', '',
	'<form action="?m=quotations&tab=0" method="post">', '</form>'
);
//finally show the titleBlock
$titleBlock->show();
//$pathEditQuote = './index.php?m=quotations&a=quote_crud';
$pathEditQuote = './index.php?m=quotations&a=quotation_edit&suppressHeaders=1';

$row = $obj->getQuotationById($uid, $quote_id);
if(!is_array($row) or count($row)==0){
	$AppUI->setMsg('Quotations');
	$AppUI->setMsg("invalidID", UI_MSG_ERROR, true);
	$AppUI->redirect("m=quotations");
}
$row = $row[0];
// format dates
$df = $AppUI->getPref('SHDATEFORMAT');
global $lock_status, $glo_currency_local;
$lock_status = 0;
$glo_currency_local = @$row['quote_currency'];
if(isset($row['quote_completed'])){
	$lock_status = intval($row['quote_completed']);
}
?>
<form name="editFrm" action="./index.php?m=quotations" method="post">
<input type="hidden" name="dosql" value="do_quote_crud" />
<input type="hidden" name="del" value="0" />
<input type="hidden" name="quote_id" value="<?=$quote_id?>" />
<input type="hidden" name="quote_status" value="<?=@$row['quote_status']?>" />
<input type="hidden" name="statusActive" value="-1" />
<input type="hidden" name="statusComplete" value="-1" />
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tbody><tr><td class="tabon">[ <?=@$row['quote_code']?> ] <?=@$row['quote_name']?></td><td width="100%"/></tr>
</tbody></table>
<table width="100%" cellspacing="0" cellpadding="7" border="0" class="infopanel">
<tbody>
	<tr>
		<td nowrap="nowrap" align="right"><b><?=$AppUI->_('Status')?>:</b></td>
		<td nowrap="nowrap"><?=@$row['status_name']?></td>
		<td nowrap="nowrap" align="right"><b><?=$AppUI->_('Type')?>:</b></td>
		<td nowrap="nowrap"><?=@$row['type_name']?></td>
		<td align="right" width="80%">
			<?php if($lock_status && $canEdit):?>
				<?php if($lock_status==ACTIVE_VAL):?>
				<input type="button" id="btnUnActiveStatus" value="<?=$AppUI->_('UnActive')?>" name="statusUnActive" onclick="return doQuotationUnActive(this.form, '<?=@$row['quote_id']?>' , '<?=@$row['quote_code']?>', '<?=@$row['quote_name']?>');"/>
				<input type="button" id="btnCompleteStatus" value="<?=$AppUI->_('Complete')?>" name="statusComplete" onclick="return doQuotationComplete(this.form, '<?=@$row['quote_id']?>' , '<?=@$row['quote_code']?>', '<?=@$row['quote_name']?>');" style="visibility: hidden;"/>
				<?php endif;?>
			<?php elseif ($canEdit):?>
				<input type="button" value="<?=$AppUI->_('Edit Quotation')?>" name="editQuotation" onclick="window.location='<?=$pathEditQuote?>&quote_id=<?=$quote_id?>'"/>
				<input type="button" id="btnActiveStatus" value="<?=$AppUI->_('Active')?>" name="statusActive" onclick="generateActiveStatus();" style="visibility: hidden;"/>
				<input type="button" id="btnCompleteStatus" value="<?=$AppUI->_('Complete')?>" name="statusComplete" onclick="return doQuotationComplete(this.form, '<?=@$row['quote_id']?>' , '<?=@$row['quote_code']?>', '<?=@$row['quote_name']?>');" style="visibility: hidden;"/>
			<?php endif;?>
		</td>
	</tr>
</tbody>
</table>
<table width="100%" cellspacing="0" cellpadding="4" border="0" class="subinfopanel">
<tbody><tr>
	<td width="50%" valign="top" height="100%">
		<table cellspacing="0" cellpadding="4" border="0" width="100%" class="list">
		<tr class="info"><td colspan="4"><?=$AppUI->_('Information Detail')?></td></tr>
		<tr class="second">
			<td align="right"><b><?=$AppUI->_('Customer')?>:</b></td>
			<td>
			<a href="<?=$pathCompanyView?>&company_id=<?=@$row['quote_customer']?>" title="<?=$AppUI->_('Company Detail')?>"><?=@$row['cus_name']?></a>
			</td>
			<td align="right"><b><?=$AppUI->_('Supplier')?>:</b></td>
			<td><?=@$row['supplier_name']?></td>
		</tr>
		<tr class="second">
			<td align="right" nowrap="nowrap"><b><?=$AppUI->_('Shipping Type')?>:</b></td>
			<td><?=@$row['shipping_type_name']?></td>
			<td align="right" nowrap="nowrap"><b><?=$AppUI->_('Active Date')?>:</b></td>
			<td>
				<?php if (isset($row['quote_start_date']) ){$d = new CDate($row['quote_start_date']); echo $d->format($df) . ' - ';}?>
				<?php if (isset($row['quote_expire_date']) ){$d = new CDate($row['quote_expire_date']); echo $d->format($df);}?>
			</td>
		</tr>
		<tr class="second">
			<td align="right" nowrap="nowrap"><b><?=$AppUI->_('Currency')?>:</b></td>
			<td colspan="100%"><?=@$row['currency_name']?></td>
		</tr>
		</table>
	</td>
	<td width="50%" valign="top" height="100%">
		<table width="100%" height="100%" cellspacing="0" cellpadding="2" border="0" class="list">
		<tr class="info"><td colspan="2"><?=$AppUI->_('Remarks')?></td></tr>
		<tr class="second">
			<td style="height: 100%;" valign="top"><div><?=@$row['quote_remarks']?></div></td>
		</tr>
		</table>
</td></tr>
</tbody>
</table>
</form>
<div id="idActiveStatusDialog" class="popup" style="width: 200px; visibility: hidden; display: none;">
	<table width="100%" cellspacing="0" cellpadding="4" border="0" class="subinfopanel">
		<tr>
			<th colspan="3">Active Date</th>
		</tr>
		<tr>
			<td align="right" nowrap="nowrap"> * <?=$AppUI->_( 'Start date' )?></td>
			<td nowrap="nowrap">
				<input id="idDateStart" type="hidden" name="quote_start_date" value=""/>
				<input id="idDateStartD" type="text" style="width: 80px; text-align: right" value=""/>
			</td>
			<td>
				<img id="idDateStartB" src="/images/calendar.gif" title="<?=$AppUI->_('Calendar')?>">
			</td>
		</tr>
		<tr>
			<td align="right" nowrap="nowrap"> * <?=$AppUI->_( 'Expire date' )?></td>
			<td nowrap="nowrap">
				<input id="idDateExpire" type="hidden" name="quote_expire_date" value=""/>
				<input id="idDateExpireD" type="text" style="width: 80px; text-align: right" value=""/>
			</td>
			<td>
				<img id="idDateExpireB" src="/images/calendar.gif" title="<?=$AppUI->_('Calendar')?>">
			</td>
		</tr>
		<tr>
			<td colspan="100%">
				<input type="button" value="<?=$AppUI->_('Active')?>" name="btnAtive" onclick="return doQuotationActive(this.form, '<?=@$row['quote_id']?>' , '<?=@$row['quote_code']?>', '<?=@$row['quote_name']?>', $('idDateStart').value, $('idDateExpire').value);"/>
				<input type="button" value="<?=$AppUI->_('Cancel')?>" name="btnCancel" onclick="Dialog('idActiveStatusDialog').hide();"/>
			</td>
		</tr>
	</table>
</div>
<script language="javascript">
Calendar.enabled = true;
</script>
<?php
// build new tab box object
$tabBox = new CTabBox('?m=quotations&a=quote_view&quote_id='.$quote_id, $tab);
if ($canEdit) {
	$tabBox->add('line_list', $AppUI->_('Line List'));
}
// Screen new line
//if($active_status){
//	if($item_id > 0)
//		$tabBox->add('line_crud', $AppUI->_('Edit Line'));
//	else
//		$tabBox->add('line_crud', $AppUI->_('New Line'));
//}
// File list
$tabBox->add('file_list', $AppUI->_('File List'));
$tabBox->show();
?>
